package de.devisnik.eidle;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.part.ViewPart;

import de.devisnik.eidle.screen.IScreen;
import de.devisnik.eidle.screen.IScreenListener;

public class EidleViewPart extends ViewPart {

	public static final String ID = EidleViewPart.class.getName();

	private AbstractScreenToggler itsScreenToggler;

	private final IScreen[] itsScreens;

	private Composite itsComposite;

	public EidleViewPart() {
		itsScreens = EidleActivator.getDefault().getScreens();
	}

	public void createPartControl(final Composite parent) {
		itsComposite = new Composite(parent, SWT.NONE);
		itsComposite.setLayout(new FillLayout());
		final ScreenHandler screenHandler = new ScreenHandler(itsComposite);
		itsScreenToggler = new AbstractScreenToggler(itsScreens, screenHandler) {
			protected boolean isScreenEnabled(final IScreen screen) {
				return EidlePreferences.isScreenEnabled(screen);
			}
		};
		screenHandler.addScreenListener(new IScreenListener() {

			public void done(IScreen screen) {
				itsScreenToggler.nextScreen();
			}
		});
		itsScreenToggler.nextScreen();

	}

	public void nextScreen() {
		itsScreenToggler.nextScreen();
	}

	public void previousScreen() {
		itsScreenToggler.previousScreen();
	}

	public IScreen getCurrentScreen() {
		return itsScreenToggler.getActiveScreen();
	}

	// @Override
	public void dispose() {
		itsScreenToggler.dispose();
		itsComposite.dispose();
		super.dispose();
	}

	public void setFocus() {
		itsComposite.setFocus();
	}

}
